﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using Friedlich.Common;
using Friedlich.RDF;
using System.Net;
using Friedlich.Delicious;
using Friedlich.Data;

namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //string rootDirectory = @"c:\users\todd\documents\visual studio 2010\Projects\Friedlich\TestConsole\";
            //TextReader xmlFile = new StreamReader(string.Concat(rootDirectory, "contacts.xml"));
            //XmlReader xmlReader = new XmlTextReader(xmlFile);
            //XDocument xmlDoc = XDocument.Load(xmlReader);
            //Friedlich.Google.FOAF foaf = new Friedlich.Google.FOAF(xmlDoc);
            //XDocument foafDoc = foaf.ToRDF();
            //foafDoc.Save(string.Concat(rootDirectory, "foaf.rdf"));

             
            Dictionary<string, DeliciousWatcher> watchers = new Dictionary<string, DeliciousWatcher>();
            string accessToken = string.Empty;

            using (Friedlich.Data.AuthorizationEntities context = new Friedlich.Data.AuthorizationEntities()) {
                RequestToken token = context.RequestTokens.FirstOrDefault<RequestToken>(x => x.UserName.Equals("tfriedlich", StringComparison.InvariantCultureIgnoreCase) && x.ApplicationName.Equals(DeliciousFactory.ApplicationName, StringComparison.InvariantCultureIgnoreCase));
                if (token != null) accessToken = token.Token;
            }
            if (!string.IsNullOrEmpty(accessToken)) {

                DeliciousWatcher deliciousWatcher = new DeliciousWatcher(accessToken);
                deliciousWatcher.PostReceived += new System.ComponentModel.AsyncCompletedEventHandler(deliciousFactory_PostReceived);
                bool quit = false;


                deliciousWatcher.ListenForPost("http://tfriedlich.posterous.com/rss.xml");
            }
            
            

        }

        static void deliciousFactory_PostReceived(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            DeliciousEventArgs args = e as DeliciousEventArgs;
            if (args != null) {
                
                DeliciousPost post = args.Post;
                DeliciousFactory factory = new DeliciousFactory();
                TagCollection myTags = factory.GetTags();

                post.Tags = new TagCollection(post.SuggestedTags.Intersect<Tag>(myTags));
            }
        }                    
                        
    }
}
